package com.azxc.rapid.plus.system.feign;

import com.azxc.rapid.core.mp.support.Condition;
import com.azxc.rapid.core.tenant.annotation.NonDS;
import com.azxc.rapid.core.tool.api.R;
import com.azxc.rapid.core.tool.utils.Func;
import com.azxc.rapid.plus.system.service.IDeptPlusService;
import com.azxc.rapid.plus.system.service.IRegionPlusService;
import com.azxc.rapid.plus.system.service.ISystemConfigPlusService;
import com.azxc.rapid.plus.system.wrapper.RegionWrapper;
import com.azxc.rapid.system.entity.Dept;
import com.azxc.rapid.system.entity.Region;
import com.azxc.rapid.system.entity.Role;
import com.azxc.rapid.system.entity.SystemConfig;
import com.azxc.rapid.system.service.IRoleService;
import com.azxc.rapid.system.vo.DeptVO;
import com.azxc.rapid.system.vo.RegionVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author linym
 * @className: SysPlusClient
 * @description:
 * @date 2022/8/23 15:51
 * @version：1.0
 */
@NonDS
@ApiIgnore
@RestController
@AllArgsConstructor
public class SysPlusClient implements ISysPlusClient {

	private final IDeptPlusService deptService;

	private final IRegionPlusService regionService;

	private final IRoleService roleService;

//	private final ISystemConfigPlusService systemConfigService;

	@Override
	public R<List<DeptVO>> getListDeptById(String deptId) {
		List<DeptVO> list = deptService.getListDeptById(deptId);
		return R.data(list);
	}

	@Override
	public R<RegionVO> getRegionVOById(String deptId) {
		Region regionVOById = regionService.getRegionVOById(deptId);
		return R.data(RegionWrapper.build().entityVO(regionVOById));
	}

	@Override
	public R<List<Region>> getListRegionById(String code) {
		List<Region> list = regionService.list(Wrappers.<Region>query().lambda().eq(Region::getParentCode, code));
		return R.data(list);
	}

	@Override
	public R<List<DeptVO>> getListDeptByRegionCode(String regionCode) {
		List<DeptVO> list = deptService.getListDeptByRegionCode(regionCode);
		return R.data(list);
	}

	@Override
	public R<String> getDeptIds(String tenantId, String deptNames) {
		List<Dept> deptList = deptService.list(Wrappers.<Dept>query().lambda().eq(Dept::getTenantId, tenantId).like(Dept::getDeptName,deptNames));
		if (deptList != null && deptList.size() > 0) {
			return R.data(deptList.stream().map(dept -> Func.toStr(dept.getId())).distinct().collect(Collectors.joining(",")));
		}
		return R.data(null);
	}

	@Override
	public R<List<DeptVO>> getDeptLazyTree(Long deptId) {
		return R.data(deptService.ChildDeptByDeptId(deptId.toString()));
	}

/*	@Override
	public R<SystemConfig> getSystemConfig(Integer type) {

		SystemConfig config = new SystemConfig();
		config.setSystemType(type);
		SystemConfig one = systemConfigService.getOne(Condition.getQueryWrapper(config));
		return R.data(one);

	}*/

	@GetMapping(GET_DEPT_BY_ID_LIST)
	@Override
	public R<List<Dept>> getDeptByIdList(String ids) {
		return R.data(deptService.listByIds(Func.toLongList(ids)));
	}

	@Override
	public R<Long> getDeptByRegionCodeAndType(String regionCode, String type) {
		return R.data(deptService.getDeptByRegionCodeAndType(regionCode, type));
	}

	@Override
	@GetMapping(DEPT_BY_PARENTID)
	public R<List<Dept>> deptByParentId(Long parentId) {
		return R.data(
			deptService.list(Wrappers.<Dept>query().lambda().eq(Dept::getParentId, parentId)
				.orderByAsc(Dept::getSort)));
	}

	@Override
	public R<Role> getRoleById(Long id) {
		return R.data(roleService.getById(id));
	}

	@Override
	public R<Role> getRoleByCode(String code) {
		return R.data(roleService.getOne(Wrappers.<Role>query().lambda().eq(Role::getRoleAlias, code)));
	}

	@Override
	public R<List<Region>> regionByType(String type, String parentCode) {
		return R.data(regionService.regionByType(type, parentCode));
	}
}
